package com.lightsh.tabhost;

import android.app.Activity;
import android.app.ActivityGroup;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

public class MyTabActivity extends ActivityGroup{
	
	
	private static final String TAG = "MyTabActivity";
	private MyTabHost mTabHost;
	
	public MyTabActivity(){
		
	}
	
	/**
	 * 获取{@link MyTabHost}对象
	 * @return
	 */
	public MyTabHost getTabHost(){
		if(mTabHost == null){
			mTabHost = (MyTabHost)findViewById(android.R.id.tabhost);
		}
		return mTabHost;
	}
	
	@Override
	public void onContentChanged() {
		Log.e(TAG, "onContentChanged");
		super.onContentChanged();
		mTabHost = (MyTabHost) findViewById(android.R.id.tabhost);
		if (mTabHost == null) {
			throw new RuntimeException("Your content must have a TabHost whose id attribute is 'android.R.id.tabhost'");
		}
		mTabHost.initHost(getLocalActivityManager());
	}

	
	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		Log.e(TAG, "onPostCreate");
	}

	@Override
	protected void onChildTitleChanged(Activity childActivity, CharSequence title) {
		Log.e(TAG, "onChildTitleChanged");
		// TODO why?
		// Dorky implementation until we can have multiple activities running.
		if (getLocalActivityManager().getCurrentActivity() == childActivity) {
			View tabView = mTabHost.getCurrentTabView();
			if (tabView != null && tabView instanceof TextView) {
				((TextView) tabView).setText(title);
			}
		}
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle state) {
		Log.e(TAG, "onRestoreInstanceState");
		super.onRestoreInstanceState(state);
	}
}
